$(document).ready(function(){
	var numUpl = new Array();
//	var picUrl = new Array();
	$("#giftInfo button[pe='button']").each(function(){
		var uploadUl = $(this).parent().find("ul").attr("id");
		var uploadBtn = $(this).attr("id");
		//原来的路径
		var picUrl= "";
		var uploader = new plupload.Uploader({
			//对运行环境的检测
			runtimes : 'html5,flash,silverlight,html4',
			//设置选择文件的标签的id；
			browse_button : uploadBtn, 
			//后台处理的路径
			url : 'upload.do',
			//内置的脚本
			flash_swf_url : '../js/Moxie.swf',
			silverlight_xap_url : '../js/Moxie.xap',
			//图片的类型和图片的最大的容量
			filters : {
				max_file_size : '10mb',
				mime_types: [
					{title : "Image files", extensions : "jpg,gif,png"},
					{title : "Zip files", extensions : "zip"}
				],
//				prevent_duplicates : true //不允许选取重复文件
			},
			
			//初始化图片上传
			init: {
				PostInit: function() {
					document.getElementById(uploadUl).innerHTML = '';

					document.getElementById(uploadBtn).onclick = function() {
						
						return false;
					};
				},
				BeforeUpload : function(up, file) {
					up.settings.url = encodeURI("./upload.do?" + "name="+ file.name+"&type="+file.type+"&size="+file.size);

				},
				//添加上传文件的操作
				FilesAdded: function(up, files) {
					//修改之前的获取图片路径
					var detailText = $("#detail").val();
					if(detailText != null && detailText != ""){
						picUrl = detailText;
					}
					//修改之前的获取图片数量
					
					var num = 0;
					
					//设置限制的图片数量
					for(i=0;i<numUpl.length;i++){
						num = num + numUpl[i];
					}
					
					var count =parseInt($("#imgNumber").val()) + uploader.files.length; 
//					alert("coufdj____))()__"+count);
				    /*if(count>8){
				        alert("最多只能上传8张图片");
				        count = $("#imgNumLater").val();
				        up.files.length = $("#imgNumLen").val();
				        return false;
				    }else if(count==0){
				    	alert("没有要上传的文件"); 
				    	return false;
				    }else{*/
			    	plupload.each(files, function(file) { 
//		    				document.getElementById(uploadUl).innerHTML += "<li id='"+file.id+"'><img id='uploadImg_"+file.id+"' name='uploadImg' src='"+file.name+"' width='50px' height='50px' /></li>";
	    				document.getElementById(uploadUl).innerHTML += "<li id='"+file.id+"'>" 
	    				+ "<div style='position:relative;'> " 
	    				+ "<div col='closes' imgNum='2' style='position:absolute;'> " 
	    				+ "<img src='t01575e539bf71e5f51.png' width='15px' /></div> " 
	    				+ "<img title='imgPic' id='uploadImg_"+file.id+"' name='uploadImg' src='"+file.name+"' width='80px' height='80px' />" 
	    				+ "</div></li>";
			    	});
			    	numUpl.push(files.length);
//				    	alert(files.length);
			    	var lenImg = $("#imgNumLen").val();
			    	$("#imgNumLen").val(parseInt(lenImg) + files.length);
			    	//一共图片数量的
			    	$("#imgNumLater").val(parseInt($("#imgNumber").val()) + parseInt($("#imgNumLen").val()));
			    	up.files.length = $("#imgNumLen").val();
			    	uploader.start(); 
			    	return false;
//				    } 
				},

				//上传的进度设置
				UploadProgress: function(up, file) {
//					document.getElementById(file.id).getElementsByTagName('span')[0].innerHTML=+ file.percent + "%";
				},
				
				//所有文件上传成功的操作
				UploadComplete : function(uploader, files) {
//					alert(files.length+"个文件上传成功");
				},
				//单个文件上传成功的操作
				FileUploaded : function(up, file,result) {
//					alert(file+"个文件999上传成功");
					var uploadImg = file.id;
					$("#uploadImg_"+uploadImg).attr("src", result.response);
					picUrl = picUrl + result.response + ",";
					
//					alert("3---"+result.response);
					
					var uploadInput = $("#"+uploadImg).parent().parent().find("input").attr("id");
					$("#"+uploadInput).val(picUrl);
//					alert("uploadInput==fff=>>///"+$("#"+uploadInput).val());
					
					$(".pictrueLi div[col='closes']").each(function(index){ 
						var aa = $(this).find("img").width();
						var bb = $(this).parent().find("img[title='imgPic']").width();
						$(this).css("left",bb-aa);
					});
				},
				//上传时的错误处理
				Error: function(up, err) {
				}
			}
		});
		
		//上传组件初始化
		uploader.init();
		
	
	});
	
	//删除图片
	$(".pictrueLi").delegate('[col="closes"]', 'click', function() {
		var aa = $(this).parent().find("img[title='imgPic']").attr("src")+",";
		var detailText = $("#detail").val();
		//去掉修改的id 
		var detailRe = detailText.replace(aa, "");
		//
		$("#detail").val(detailRe);
		$(this).parent().parent().remove();
		//删除图片的数量
		if($(this).attr("imgNum")=="1"){
			$("#imgNumber").val(parseInt($("#imgNumber").val()) - 1);
		}
		if($(this).attr("imgNum")=="2"){
			$("#imgNumLen").val(parseInt($("#imgNumLen").val()) - 1);
		}
		$("#imgNumLater").val(parseInt($("#imgNumber").val())+parseInt($("#imgNumLen").val()));
	});
});